package thread.single;

/**
 * @author Dianzhi Han
 * @version 1.0
 * @description: 懒汉单例-第一个调用getSingleTon方法才创建对象
 * @date 2022/10/30 17:28
 */
public class LazySingleTon {
    private static volatile LazySingleTon singleTon;

    private LazySingleTon() {
        int x = 10;
        int y = 20;
        int z = 30;
    }
    // 第一次调用获取单例对象方法时才实例化对象
    public static LazySingleTon getSingleTon() {
        if (singleTon == null) {
            // 初始化对象
            synchronized (LazySingleTon.class) {
                if (singleTon == null) {
                    singleTon = new LazySingleTon();
                }
            }
        }
        return singleTon;
    }
}